﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections;

using YT.DB;
using YT.Modules;
using YT.Commons; 

namespace YT.XH.NOSE
{
    public partial class frmNS12_17QOL : Form
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrSeqNo = string.Empty;
        private string mstrSickRecID = string.Empty;
        private string mstrCurPnl = string.Empty;
        ArrayList marrlist = new ArrayList();
        ArrayList marrPnl = new ArrayList();
        ArrayList marrAllPnls = new ArrayList();
        ArrayList marrMsg = new ArrayList();
        private bool mblnExit = false;
        
        public frmNS12_17QOL()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        public frmNS12_17QOL(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }

        public frmNS12_17QOL(string strSickRecID,string strSeqNo)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mstrSeqNo = strSeqNo;
        }

        private void frmNS12_17QOL_Load(object sender, EventArgs e)
        {
            FillData();
            timer1.Start();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            mblnExit = true;
            if (UpdateData())
                this.Close();
        }

        private void frmNS12_17QOL_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
            frmGifImage frmGIF = new frmGifImage();
            frmGIF.TopMost = true;
            frmGIF.Show();
        }

        private void FindPanel(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.GetType().Name == "Panel")
                {
                    marrAllPnls.Add(con);
                }
                if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                {
                    marrlist.Add(con);
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
            }
        }

        private void FillData()
        {

            string strTableName = "ExamBeforTreatResult";

            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
            }

            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            string strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and ExamSeqNo='"+ mstrSeqNo  +"' and SickRecID='" + mstrSickRecID + "' and Type='" + TitleType.NOSE + "'";
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null) continue;
                            strResult = CvtFunc.ToString(drCheck["Result"]);
                            if (conText.GetType().Name == "TextBox")
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                if (strResult != "") ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult);
                            }
                            else if (conText.GetType().Name == "DateTimeEx")
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                continue;
                            }
                        }
                    }
                }
            }
            dbcon.CloseCon();
        }

        private bool UpdateData()
        {
            try
            {
                string strTableName = "ExamBeforTreatResult";
                DBAccess dbcon = new DBAccess(false, false);
                if (mstrSeqNo == "" || mstrSeqNo == "0")
                {
                    mstrSeqNo = (CvtFunc.ToInt(DataFunctions.GetFieldValue(strTableName, "max(ExamSeqNo) as SeqNo", " 1=1 and Type='" + TitleType.NOSE + "' ")) + 1).ToString();
                    string strSQL = string.Empty;
                    strSQL = " INSERT INTO ExamBeforeTreathdr (SickRecID,SeqNo,ExamDate,Remark,Type) " +
                             " VALUES ( '{0}','{1}','{2}','{3}','{4}')";
                    strSQL = string.Format(strSQL, mstrSickRecID, mstrSeqNo, dateTimePicker1.Value, "", TitleType.NOSE);
                    dbcon.ExecuteSql(strSQL);
                             
                }

                foreach (Control conDtl in marrlist)
                {
                    if (conDtl.GetType().Name == "Panel")
                    {
                        string[] strInfoSeqNo = new string[2];
                        strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                        string strHdrSeqNo = strInfoSeqNo[0];
                        string strDtlSeqNo = strInfoSeqNo[1];
                        string strSickRecID = mstrSickRecID;
                        string strColTitle = string.Empty;
                        string strResult = string.Empty;
                        DataRow drRow = null;
                        string strFields = "SickRecID,HdrSeqno,DtlSeqNo,ExamSeqNo,RowNo,ColTitle,Result,Type";
                        foreach (Control conText in conDtl.Controls)
                        {
                            if (conText.Tag != "" && conText.Tag != null)
                            {
                                if (conText.GetType().Name == "TextBox")
                                {
                                    strResult = conText.Text;
                                }
                                else if (conText.GetType().Name == "DateTimePicker")
                                {
                                    strResult = BaseFunctions.FormatDate(((DateTimePicker)conText).Value.ToString(), DateFormatOption.DateFmtYMD);
                                }
                                else if (conText.GetType().Name == "DateTimeEx")
                                {
                                    if (conText.Text == "    -  -  ")
                                        strResult = "";
                                    else
                                        strResult = conText.Text;
                                }
                                else if (conText.GetType().Name == "RadioButton")
                                {
                                    strResult = (((RadioButton)conText).Checked ? "true" : "false");
                                }
                                else if (conText.GetType().Name == "CheckBox")
                                {
                                    strResult = (((CheckBox)conText).Checked ? "true" : "false");
                                }
                                else if (conText.GetType().Name == "Label")
                                {
                                    strResult = "";
                                    continue;
                                }
                                strColTitle = conText.Tag.ToString();

                                string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID ='" + mstrSickRecID + "' and Type='" + TitleType.NOSE  + "'";
                                bool blnIsNew = false;
                                DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                                if (drCheck == null)
                                {
                                    blnIsNew = true;
                                    drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                                }
                                else
                                {
                                    blnIsNew = false;
                                    drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                                }

                                drRow["SickRecID"] = strSickRecID;
                                drRow["HdrSeqno"] = strHdrSeqNo;
                                drRow["DtlSeqNo"] = strDtlSeqNo;
                                drRow["RowNo"] = strColTitle;
                                drRow["Result"] = strResult;
                                drRow["Type"] = TitleType.NOSE;
                                drRow["ExamSeqNo"] = mstrSeqNo; 
                                if (blnIsNew)
                                    DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                                else
                                    DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                            }
                        }
                    }
                }
                dbcon.CloseCon();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            return true;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void btnXC_Click(object sender, EventArgs e)
        {
            frmNSasthmaControlMtn frmXCMtn = new frmNSasthmaControlMtn();
            frmXCMtn.ShowDialog(); 
        }

        private void frmNS12_17QOL_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!mblnExit)
            {
                if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (!UpdateData()) e.Cancel = true;
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            mblnExit = true;
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (!UpdateData()) return;
            }
            this.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("您是否确定要删除？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DBAccess dbcon = new DBAccess(false, false);
                string strSQL = "Delete ExamBeforeTreathdr where SickRecID='" + mstrSickRecID + "' and SeqNo='" + mstrSeqNo + "'";
                dbcon.ExecuteSql(strSQL);
                strSQL = "Delete ExamBeforTreatResult where  SickRecID='" + mstrSickRecID + "' and ExamSeqNo='" + mstrSeqNo + "'";
                dbcon.ExecuteSql(strSQL);
                mblnExit = true;
                this.Close();
            }
        }
    }
}
